package com.bjfu.gmms.servlet;

import com.bjfu.gmms.MyBatisUtil;
import com.bjfu.gmms.mapper.MentorQualifiedDirectionsMapper;
import com.bjfu.gmms.model.MentorQualifiedDirections;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/mentorDirections")
public class MentorDirectionsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession sqlSession = null;
        try {
            // 获取 MyBatis 的 SqlSession
            sqlSession = MyBatisUtil.getSqlSession("development");
            MentorQualifiedDirectionsMapper mapper = sqlSession.getMapper(MentorQualifiedDirectionsMapper.class);

            // 调用 Mapper 获取数据
            List<MentorQualifiedDirections> mentors = mapper.getAllQualifiedMentors();

            // 将数据存储到请求中并转发到 JSP 页面
            request.setAttribute("mentors", mentors);
            RequestDispatcher dispatcher = request.getRequestDispatcher("mentorDirections.jsp");
            dispatcher.forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}
